package com.jml.zlhq.app.adapter;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.util.UIUtil.OnTagClick;

public class ConditionAdapter extends IBaseAdapter {
	private List<JSONObject> itemList;
	private AppContext appContext;
	private Activity activity;
	private HashMap<String, String> param;
	private Map<Integer,LinkedList<View>> clist;
	private Map<String,String> condition;

	public ConditionAdapter(Activity activity, List<JSONObject> itemList, Map<String,String> condition) {
		this.itemList = itemList;
		this.appContext = (AppContext) activity.getApplication();
		this.activity = activity;
		this.condition = condition;
		this.param = new HashMap<String, String>();
		this.clist = new HashMap<Integer,LinkedList<View>>();
	}
	
	public Map<String, String> getParam(){
		return param;
	}

	@Override
	public int getCount() {
		int count = itemList == null ? 0 : itemList.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
//	@Override
//	public int getItemViewType(int position) {
//		int type = 0;
//		if(position==itemCount){
//			type=1;
//		}else{
//			type=0;
//		}
//		return type;
//
//	}
//
//	@Override
//	public int getViewTypeCount() {
//		return 2;
//	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
//		if(position==itemCount){
//			convertView = createNav(position, convertView, parent);
//		}else{
			convertView = createItem(position, convertView, parent);
//		}
		return convertView;
	}
	
	
	static class ItemHolder {
		public TextView title;
		public RelativeLayout content;
		public LinearLayout bottom;
		public Button reset,submit;
	}
	private View createItem(int position, View convertView, final ViewGroup parent){
		ItemHolder holder = null;
		if(convertView==null){
			holder = new ItemHolder();
			int condition_item = R.layout.condition_item;
			convertView = LayoutInflater.from(appContext).inflate(condition_item, null);
			holder.title = (TextView) convertView.findViewById(R.id.conditon_item_title);
			holder.content = (RelativeLayout) convertView.findViewById(R.id.conditon_item_content);
			holder.reset = (Button) convertView.findViewById(R.id.condition_item_reset);
			holder.submit = (Button) convertView.findViewById(R.id.condition_item_submit);
			holder.bottom = (LinearLayout) convertView.findViewById(R.id.conditon_item_bottom);
			convertView.setTag(holder);
		}else{
			holder = (ItemHolder) convertView.getTag();
		}
		JSONObject json = itemList.get(position);
		holder.bottom.setVisibility(position==itemList.size()-1?View.VISIBLE:View.GONE);
//		@SuppressWarnings("unchecked")
//		Iterator<String> keyIter= json.keys();  
//        while (keyIter.hasNext()) {  
//        	String key = keyIter.next();
//        	 if(!key.equals("key")){
        		 try {
        			 holder.title.setText(BaseTools.getFrom("label", json));
        			 String cdtd = null;
        			 try {
        				 cdtd = condition.get(BaseTools.getFrom("key", json));
					} catch (Exception e) {}
        			 LinkedList<View> tagviews = UIUtil.autoTag(activity, json.getJSONArray("values"), BaseTools.getFrom("key", json), cdtd, holder.content, new OnTagClick() {
        					@Override
        					public void onTagClick(Map<String,String> v) {
        						param.putAll(v);
        					}
        				});
        			 clist.put(position, tagviews);
        			// LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,Integer.parseInt(holder.content.getTag(1).toString()));
        			 //holder.content.setLayoutParams(lp);
        		 } catch (Exception e) {
					e.printStackTrace();
        		 }
//        		 break;
//        	 }
//        }
        if(position==itemList.size()-1){
        	holder.reset.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					clist.clear();
					param.clear();
					if(condition!=null)condition.clear();
					notifyDataSetChanged();
//					for (Entry<Integer, LinkedList<View>> e : clist.entrySet()) {
//						LinkedList<View> tvw= e.getValue();
//						if(tvw!=null&&tvw.size()>0){
//							tvw.get(0).performClick();
//						}
//					}
				}
			});
        	holder.submit.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.putExtra("data", param);
					activity.setResult(Activity.RESULT_OK,intent);
					activity.finish();
				}
			});
        }else{
        	holder.reset.setOnClickListener(null);
        	holder.submit.setOnClickListener(null);
        }
		
		return convertView;
	}
}
